import { createRouter, createWebHashHistory } from 'vue-router'
import Layout from '@/layout/index.vue'
export const constantRoutes = [{
  path: '/login',
  component: () => import('@/views/login/index.vue'),
  hidden: true
}, {
  path: '/404',
  component: () => import('@/views/404.vue'),
  hidden: true
}, {
  path: '/',
  component: Layout,
  redirect: '/home',
  children: [{
    path: 'home',
    name: 'Home',
    component: () => import('@/views/dashboard/index.vue'),
    meta: { title: '首页', icon: 'el-icon-s-home' }
  }]
}, {
  path: '/example',
  component: Layout,
  redirect: '/example/table',
  name: 'Example',
  meta: { title: '表格组件', icon: 'el-icon-s-management' },
  children: [{
    path: 'table',
    name: 'Table',
    component: () => import('@/views/table/index.vue'),
    meta: { title: '表格', icon: 'el-icon-s-grid' }
  }, {
    path: 'tree',
    name: 'Tree',
    component: () => import('@/views/tree/index.vue'),
    meta: { title: '树形组件', icon: 'el-icon-s-shop' }
  }]
}, {
  path: '/form',
  component: Layout,
  children: [{
    path: 'index',
    name: 'Form',
    component: () => import('@/views/form/index.vue'),
    meta: { title: '表单', icon: 'el-icon-tickets' }
  }]
}, {
  path: '/nested',
  component: Layout,
  redirect: '/nested/menu1',
  name: 'Nested',
  meta: {
    title: '多级菜单',
    icon: 'el-icon-folder'
  },
  children: [{
    path: 'menu1',
    component: () => import('@/views/nested/menu1/index.vue'),
    name: 'Menu1',
    meta: { title: '三级菜单' },
    children: [{
      path: 'menu1-1',
      component: () => import('@/views/nested/menu1/menu1-1/index.vue'),
      name: 'Menu1-1',
      meta: { title: 'Menu1-1' }
    }, {
      path: 'menu1-2',
      component: () => import('@/views/nested/menu1/menu1-2/index.vue'),
      name: 'Menu1-2',
      meta: { title: 'Menu1-2' },
      children: [{
        path: 'menu1-2-1',
        component: () => import('@/views/nested/menu1/menu1-2/menu1-2-1/index.vue'),
        name: 'Menu1-2-1',
        meta: { title: 'Menu1-2-1' }
      }, {
        path: 'menu1-2-2',
        component: () => import('@/views/nested/menu1/menu1-2/menu1-2-2/index.vue'),
        name: 'Menu1-2-2',
        meta: { title: 'Menu1-2-2' }
      }]
    }, {
      path: 'menu1-3',
      component: () => import('@/views/nested/menu1/menu1-3/index.vue'),
      name: 'Menu1-3',
      meta: { title: 'Menu1-3' }
    }]
  }, {
    path: 'menu2',
    component: () => import('@/views/nested/menu2/index.vue'),
    name: 'Menu2',
    meta: { title: '一级菜单' }
  }]
}, {
  path: '/external-link',
  component: Layout,
  children: [{
    path: 'https://cn.vitejs.dev/guide/why.html',
    meta: { title: '外链', icon: 'el-icon-link' }
  }]
}, { path: '/:pathMatch(.*)', redirect: '/404', hidden: true }]
const DynamicCreateRouter = () => createRouter({
  history: createWebHashHistory(),
  scrollBehavior(to, from, savedPosition) {
    // 始终滚动到顶部
    return { top: 0 }
  },
  routes: constantRoutes
});
const router = DynamicCreateRouter()
// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = DynamicCreateRouter()
  router.matcher = newRouter.matcher // reset router
}
export default router
